﻿@{
    ViewData["Title"] = "Create";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , parentFrameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引;

            $.getJSON("/Console/RebateLevel/GetSymbols", function (result) {
                let plat = result.data;
                $('#Names').append(new Option(top.lang.pleaseSelect, ""));
                plat.forEach(function (v) {
                    $('#Names').append(new Option(v.text, v.id));
                });
                layui.form.render("select");
            });

            /**多语言初始化 */
            top.SetFormPageLanguage(layui);
            /* 监听提交 */
            form.on('submit(submit-form)', function (data) {
                if ($("#Names").val() == "") {
                    top.AlertByKey(layui, "selectVariety", 0);
                    return false;
                }
                var Cardinal = $("#Cardinal").val().trim().replace(/,/g, '');
                if (Cardinal == "") {
                    top.AlertByKey(layui, "enterCardinal", 0);
                    return false;
                } else if (isNaN(Cardinal)) {
                    top.AlertByKey(layui, "cardinalNotIsNumber", 0);
                    return false;
                } else if (Number(Cardinal) <= 0) {
                    top.AlertByKey(layui, "pleaseEnterThanZero", 0);
                    return false;
                }
                if ($("#Sort").val() == "") {
                    top.AlertByKey(layui, "enterSort", 0);
                    return false;
                } else if (isNaN($("#Sort").val())) {
                    top.AlertByKey(layui, "sortNotIsNumber", 0);
                    return false;
                }
                var datas = data.field;
                datas.Cardinal = $("#Cardinal").val().trim().replace(/,/g, '');
                $.ajax({
                    type: "POST",
                    url: "/Console/RebateLevel/Create",
                    data: datas, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            /**提示语弹框 */
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                parent.layer.close(parentFrameIndex);
                                parent.renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, item.data, 0);
                        }
                        layer.closeAll('loading');
                    }
                });
                return false;
            });
            //光标离开输入的金额四舍五入并保留4位小数
            $("#Cardinal").blur(function () {
                var obj = this.value;
                var mnyReg = /^([1-9][0-9]*|(([0]\.[0-9]{0,2}[1-9]\d*|[1-9][0-9]*\.\d*)))$/;//根据需求修改
                if (!(mnyReg.test(obj))) {
                    top.AlertByKey(layui, "enterNotIsNumber", 0);
                    return;
                }
                var n = 4; //保留得小数位
                obj = parseFloat(obj).toFixed(n);
                var left = obj.split(".")[0].split("").reverse();
                var right = obj.split(".")[1];
                var total = new Array();
                for (i = 0; i < left.length; i++) {
                    total.push(left[i]);
                    if ((i + 1) % 3 == 0 && (i + 1) != left.length) {
                        total.push(",");
                    }
                }
                $("#Cardinal").val(total.reverse().join("") + "." + right);
            });

            //光标进入，去除金额千分位，并去除小数后面多余的0
            $("#Cardinal").focus(function () {
                var oldMny = this.value.replace(/,/g, '');
                if (oldMny.indexOf(".") > 0) {
                    oldMny = oldMny.replace(/0+?$/, "");//去除尾部多余的0
                    oldMny = oldMny.replace(/[.]$/, "");//如果最后一位是.则去掉
                }
                this.value = oldMny;
            });
        });
    </script>
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group">
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="variety">品种</label>
                    <div class="layui-input-block">
                        <select name="Names" id="Names" lay-filter="Names">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="cardinal">返佣基数</label>
                    <div class="layui-input-block">
                        <input type="text" name="Cardinal" id="Cardinal" placeholder="enterCardinal" class="layui-input">
                        <input id="Pid" name="Pid" value="@ViewBag.Pid" type="hidden" />
                        <input id="SchemeId" name="SchemeId" value="@ViewBag.schemeId" type="hidden" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="sort">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="Sort" id="Sort" value="@ViewBag.Sort" placeholder="sort" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label" lay-event="remark">备注</label>
                    <div class="layui-input-block">
                        <textarea name="Remark" id="Remark" placeholder="remark" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button id="submitForm" class="layui-btn" lay-submit="" lay-filter="submit-form">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>